<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 服务接口
 */
class Service extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getServiceList()
    {
        if ($this->request->isPost()) {
            $pageIndex = $this->request->post('pageIndex', 1);
            $pageSize = $this->request->post('pageSize', 10);
            $typeId = $this->request->post('typeId');
            $status = $this->request->post('status');
            $where = [];
            if ($typeId) {
                $where['type_id'] = $typeId;
            }
            if ($status) {
                $where['status'] = $status;
            }
            $list = model('\app\admin\model\jy\Service')
                ->where("is_del = 0 and `status` in ('1', '2')")
                ->where($where)
                ->order('weigh desc, update_time desc')
                ->paginate($pageSize, false, ['page' => $pageIndex]);
            foreach ($list->items() as &$item) {
                if ($item['cover_image']) {
                    $item['cover_image'] = cdnurl($item['cover_image']);
                }
                $imagesList = [];
                if ($item['effect_images']) {
                    $imagesStrList = explode(',', $item['effect_images']);
                    $item['imageCount'] = sizeof($imagesStrList);
                    if ($imagesStrList) {
                        foreach ($imagesStrList as &$image) {
                            $imagesList[] = cdnurl($image);
                        }
                    }
                }
                $item['imageList'] = $imagesList;
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            $this->success('', $result);
        }
    }

    public function getServiceById($serviceId = NULL)
    {
        if (!$serviceId) {
            $this->error(__('Parameter %s can not be empty', 'serviceId'));
        }
        $service = model('\app\admin\model\jy\Service')
            ->where(['id' => $serviceId, 'is_del' => 0])
            ->whereIn('status', [1, 2])
            ->find();
        if (!$service) {
            $this->error(__('服务不可用'), null, 999);
        }
        if ($service['cover_image']) {
            $service['cover_image'] = cdnurl($service['cover_image']);
        }
        $imagesList = [];
        if ($service['effect_images']) {
            $imagesStrList = explode(',', $service['effect_images']);
            if ($imagesStrList) {
                foreach ($imagesStrList as &$image) {
                    $imagesList[] = cdnurl($image);
                }
            }
        }
        $service['imageList'] = $imagesList;
        // 获取服务铺贴方式
        $pavingTypeList = model('\app\admin\model\jy\ServicePavingType')->alias('jspt')
            ->field('jspt.*')
            ->join('jy_service js', 'js.id = jspt.service_id')
            ->where(['js.id' => $serviceId])
            ->where('js.status != "0"')
            ->order('jspt.weigh Desc, jspt.create_time desc')
            ->select();
        $service['pavingTypeList'] = $pavingTypeList;
        $this->success('', $service);
    }
}